package com.slj.controller;

import com.slj.entity.ProductTypes;
import com.slj.service.ProductTypesService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/product-types")
@CrossOrigin(origins = "*")
@RequiredArgsConstructor
public class ProductTypesController {

    @Autowired
    private ProductTypesService productTypesService;

    /**
            * 添加商品类型
     */
    @PostMapping
    public ResponseEntity<?> addProductType(@RequestBody ProductTypes productType) {
        int result = productTypesService.insert(productType);
        return result > 0
                ? ResponseEntity.ok().body("商品类型添加成功")
                : ResponseEntity.badRequest().body("商品类型添加失败");
    }

    /**
            * 根据ID获取商品类型
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> getProductTypeById(@PathVariable Integer id) {
        ProductTypes productType = productTypesService.selectByPrimaryKey(id);
        return productType != null
                ? ResponseEntity.ok(productType)
                : ResponseEntity.notFound().build();
    }

    /**
            * 获取所有商品类型
     */
    @GetMapping
    public ResponseEntity<List<ProductTypes>> getAllProductTypes() {
        List<ProductTypes> productTypes = productTypesService.selectAll();
        return ResponseEntity.ok(productTypes);
    }

    /**
            * 更新商品类型
     */
    @PutMapping
    public ResponseEntity<?> updateProductType(@RequestBody ProductTypes productType) {
        int result = productTypesService.updateByPrimaryKey(productType);
        return result > 0
                ? ResponseEntity.ok().body("商品类型更新成功")
                : ResponseEntity.badRequest().body("商品类型更新失败");
    }

    /**
            * 删除商品类型
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteProductType(@PathVariable Integer id) {
        int result = productTypesService.deleteByPrimaryKey(id);
        return result > 0
                ? ResponseEntity.ok().body("商品类型删除成功")
                : ResponseEntity.badRequest().body("商品类型删除失败");
    }
}